

<!DOCTYPE html>
<html lang="null">
<head prefix="og: http://ogp.me/ns#">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="保存一下自己积累的技术资料">
    <title>python生成器 | 黄龙天空</title>

    <!-- Open Graph -->
    <meta name="description" content="&amp;#x57FA;&amp;#x4E8E;yield&amp;#x8BED;&amp;#x53E5;&amp;#xFF0C;&amp;#x751F;&amp;#x6210;&amp;#x5668;&amp;#x53EF;&amp;#x4EE5;&amp;#x6682;&amp;#x505C;&amp;#x51FD;&amp;#x6570;&amp;#x5E76;&amp;#x8FD4;&amp;#x56DE;&amp;#x4E00;&amp;#x4E2A;&amp;#x4E2D;&amp;#x95F4;&amp;#x7ED3;&amp;#x679C;&amp;#x3002;&amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="python生成器">
<meta property="og:url" content="http://huanglongtiankong.oschina.io/2018/06/28/yield/index.html">
<meta property="og:site_name" content="黄龙天空">
<meta property="og:description" content="&amp;#x57FA;&amp;#x4E8E;yield&amp;#x8BED;&amp;#x53E5;&amp;#xFF0C;&amp;#x751F;&amp;#x6210;&amp;#x5668;&amp;#x53EF;&amp;#x4EE5;&amp;#x6682;&amp;#x505C;&amp;#x51FD;&amp;#x6570;&amp;#x5E76;&amp;#x8FD4;&amp;#x56DE;&amp;#x4E00;&amp;#x4E2A;&amp;#x4E2D;&amp;#x95F4;&amp;#x7ED3;&amp;#x679C;&amp;#x3002;&amp;#x">
<meta property="og:updated_time" content="2018-06-28T09:17:35.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python生成器">
<meta name="twitter:description" content="&amp;#x57FA;&amp;#x4E8E;yield&amp;#x8BED;&amp;#x53E5;&amp;#xFF0C;&amp;#x751F;&amp;#x6210;&amp;#x5668;&amp;#x53EF;&amp;#x4EE5;&amp;#x6682;&amp;#x505C;&amp;#x51FD;&amp;#x6570;&amp;#x5E76;&amp;#x8FD4;&amp;#x56DE;&amp;#x4E00;&amp;#x4E2A;&amp;#x4E2D;&amp;#x95F4;&amp;#x7ED3;&amp;#x679C;&amp;#x3002;&amp;#x">

    <script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "email": "mailto:1300896173@qq.com",
  "image": "undefined",
  "name": "黄龙天空",
  "url": "http://huanglongtiankong.oschina.io"
}
</script>
    <script>
    var algoliaEnabled = false;
    
</script>

    

    
    
        <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
    
    
    
        <!-- stylesheets list from config.yml -->
        
        <link rel="stylesheet" href="/css/aloha.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/magnific-popup.js/1.1.0/magnific-popup.min.css">
        
    

</head>
<body id="body" style="margin-bottom: 0;" class="pushable">

    <div class="ui top fixed menu">
        <a class="item" id="menu-icon"><i class="sidebar icon"></i></a>
    </div>

    <div id="menu-sidebar" class="ui left vertical sidebar menu">

    <div id="sidebar-top">
        <div class="content">
            <h3>黄龙天空</h3>
        </div>
    </div>
    <div class="ui container sidebar-card">
    <div class="ui people shape content">
        <div class="active side">
            <div class="ui card">
                <div class="image">
                    <img class="ui medium bordered image" src="/images/avatar.jpg">
                </div>
                <div class="content">
                    <a class="header">黄龙天空</a>
                    
                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                        
                        <div class="item">
                            <i class="mail icon" style="float: left"></i>
                            <div class="content">
                                <a href="mailto:1300896173@qq.com">1300896173@qq.com</a>
                            </div>
                        </div>
                        
                    </div>

                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>

    
    

    

    
    <a href="/index.html" class="item" >
        <i class="home icon"></i>
        首页
    </a>

    

    
</div>

    <div class="pusher body-content">
        <div id="content" class="ui main container">
            <!--<div class="ui one column grid">-->
                <!--<div class="column">-->
                    <!--<div class="ui main container">-->
                        <div id="post-yield" class="ui main container article-type-post">

    

    <div class="ui divided grid">

        <div class="two column row" id="article-content">

            <div class="thirteen wide computer thirteen wide tablet sixteen wide mobile column">
                

<h1 class="ui header">
    
    python生成器
</h1>



                <div class="article-inner">

                    <div class="article-entry" itemprop="articleBody">
                        
                        <p>&#x57FA;&#x4E8E;yield&#x8BED;&#x53E5;&#xFF0C;&#x751F;&#x6210;&#x5668;&#x53EF;&#x4EE5;&#x6682;&#x505C;&#x51FD;&#x6570;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x7ED3;&#x679C;&#x3002;&#x4EE3;&#x7801;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>In [1]: def fibonacci():
            a, b=0, 1
            while True:
                yield b
                a, b = b, a+b

In [2]: fib = fibonacci()

In [3]: next(fib)
Out[3]: 1

In [4]: next(fib)
Out[4]: 1

In [5]: next(fib)
Out[5]: 2

In [6]: [next(fib) for i in range(10)]
Out[6]: [3, 5, 8, 13, 21, 34, 55, 89, 144, 233]

fibonacci&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x662F;&#x7279;&#x6B8A;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C;&#x5B83;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x4FDD;&#x5B58;&#x6267;&#x884C;&#x4E0A;&#x4E0B;&#x6587;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x88AB;&#x65E0;&#x9650;&#x6B21;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x6BCF;&#x6B21;&#x90FD;&#x4F1A;&#x751F;&#x6210;&#x5E8F;&#x5217;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x3002;
</code></pre><p>python&#x751F;&#x6210;&#x5668;&#x8FD8;&#x80FD;&#x591F;&#x5229;&#x7528;next&#x51FD;&#x6570;&#x4E0E;&#x8C03;&#x7528;&#x7684;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#x3002;yield&#x53D8;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x800C;&#x503C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x540D;&#x4E3A;send&#x7684;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x4F20;&#x9012;</p>
<pre><code>In [7]: def a():
            s = 1
            while True:
                print(s)
                s = (yield)

In [8]: b = a()

In [9]: next(b)
1

In [10]: b.send(&apos;&#x7ED9;s&#x8D4B;&#x503C;&apos;)
&#x7ED9;s&#x8D4B;&#x503C;
</code></pre>
                        
                    </div>

                    
                    
                    

                    <div class="description post-description">
    <span class="post-description-item">
        <time datetime="2018-06-28T08:29:33.000Z" itemprop="datePublished">2018-06-28</time>

    </span>
    <span class="post-description-item">
        

    <span>
        <i class="tags icon"></i>
    </span>

    
    <span class="ui tiny label">
        <a href="/tags/yield/">yield</a>
    </span>

    

    </span>
</div>

                </div>
                
                <div id="pagination" class="nav-web ui text container pagination">

    <div class="ui stackable two column divided grid container">
        <div class="row">
            <div class="column nav-left">
                

                <a class="ui tiny button disabled"><i class="angle left icon"></i>前一篇</a>

                
            </div>
            <div class="column nav-right">
                

                <a href="/2017/05/17/python使用小技巧/" class="ui tiny button">
                    
                    python使用小技巧
                    
                    <i class="angle right icon"></i></a>
                
            </div>
        </div>
    </div>

</div>
                
            </div>

            
            <div class="three wide computer three wide tablet column">

                <div class="ui sticky article-toc" id="article-toc">
                    <!-- toc -->
                    
                </div>

            </div>
            

        </div>
    </div>
</div>


                    <!--</div>-->
                <!--</div>-->
            <!--</div>-->
        </div>
        <div class="ui vertical footer segment">
    <div class="ui center aligned container">
        <div class="ui inverted section divider"></div>
        &copy; 2018 <a href="/">黄龙天空</a>,
        Powered by <a href="https://github.com/henryhuang/hexo-theme-aloha" target="_blank">Aloha</a> and <a
                href="http://hexo.io/" target="_blank">Hexo</a>.
    </div>
</div>

    </div>




<!-- scripts list from theme config.yml -->

<script src="//cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>

<script src="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch/3.18.1/algoliasearch.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch-helper-js/2.13.0/algoliasearch.helper.min.js"></script>

<script src="/js/semantic-ui-algolia.js"></script>

<script src="/js/aloha-events.js"></script>



</body>
</html>
